package io.renren.modules.course.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.course.service.CourseClassificationService;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.course.entity.CourseEntity;
import io.renren.modules.course.service.CourseService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import redis.clients.jedis.Jedis;


/**
 * 技术名称列表
 *
 * @author zlb
 * @email 316682546@qq.com
 * @date 2018-07-04 09:29:28
 */
@RestController
@RequestMapping("course/course")
public class CourseController extends AbstractController {
    @Autowired
    private CourseService courseService;

    /**
     * 列表
     */
    @RequestMapping("/list")
   // @RequiresPermissions("course:course:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = courseService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/getMyCourselist")
   // @RequiresPermissions("course:course:list")
    public R getMyCourselist(@RequestParam Map<String, Object> params){
        params.put("userId",getUserId());
        PageUtils page = courseService.getMyCourselist(params);


        /*Jedis jedis = new Jedis("106.14.146.131");
        jedis.del("courseId*");*/
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
   // @RequiresPermissions("course:course:info")
    public R info(@PathVariable("id") Integer id){
        CourseEntity course = courseService.selectById(id);

        return R.ok().put("course", course);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
   // @RequiresPermissions("course:course:save")
    public R save(@RequestBody CourseEntity course){
        course.setTeacherId(getUserId().intValue());
        if(
                course.getTitle()!=null &&
                course.getIntroduce()!=null &&
//                course.getIntegral()!=0 &&
                course.getCourseMustKnow()!=null &&
                course.getTitle()!=null &&
                course.getCoverPath() != null) {
            course.setCreateTime(new Date());
            courseService.insert(course);
            return R.ok();
        }
        return R.ok("请完善课程信息",101);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("course:course:update")
    public R update(@RequestBody CourseEntity course){
        ValidatorUtils.validateEntity(course);
        courseService.updateAllColumnById(course);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("course:course:delete")
    public R delete(@RequestBody Integer[] ids){
        courseService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }



    /**
     * 删除
     */
    @RequestMapping("/online")
   // @RequiresPermissions("course:course:delete")
    public R online(@RequestBody Map map){
      //  courseService.deleteBatchIds(Arrays.asList(ids));
            courseService.online(map);
        return R.ok();
    }

}
